package com.auto.learning.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.auto.learning.bean.BannerItem;
import com.auto.learning.bean.Banner_H5;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.BookListModel;
import com.auto.learning.bean.BookTypeBean;
import com.auto.learning.bean.CacheTypeBean;
import com.auto.learning.bean.ListenModeBean;
import com.auto.learning.bean.ReadModeBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.SelectionVo;
import com.auto.learning.bean.UserBaseInfo;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.bean.UserVipInfo;
import com.auto.learning.bean.VideoBean;
import com.auto.learning.event.Handle;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.store.core.table.UserInfoTable;
import com.auto.learning.thread.pool.Pool;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.view.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.dao.RawRowMapper;

public class Cache {

	/**
	 * 将 BookBean 缓存到本地
	 * @param list
	 */
	public static void cacheBook2Local(final ArrayList<BookBean> list,final boolean isGuessYouLike) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				try {
					if (list != null && list.size() > 0) {
						if (isGuessYouLike) {
							List<CacheTypeBean> cacheTypeBeans = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("type", 0).query();
							if (cacheTypeBeans != null && cacheTypeBeans.size() > 0) {
								StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).delete(cacheTypeBeans);
							}
						}
						for (BookBean bookBean : list) {
							cacheBookBean(bookBean);
							if (isGuessYouLike) {
								CacheTypeBean cacheTypeBean = new CacheTypeBean();
								cacheTypeBean.setBookId(bookBean.getBookId());
								cacheTypeBean.setType(0);
								StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).createOrUpdate(cacheTypeBean);
							}else {
								List<CacheTypeBean> cacheTypeBeans = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("type", 0).and().eq("bookId", bookBean.getBookId()).query();
								if (cacheTypeBeans != null && cacheTypeBeans.size() > 0) {
									StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).delete(cacheTypeBeans);
								}
							}
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static void cacheChoosePageList2Local(final ArrayList<BookBean> list){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				if (list != null) {
					for (BookBean bookBean : list) {
						cacheBookBean(bookBean);
					}
				}
			}
		});
	}
	
	/**
	 * 缓存最新或者周top10到本地
	 * @param list
	 * @param cacheType
	 */
	public static void cacheNewOrTopBookList2Local(final ArrayList<BookBean> list,final int cacheType){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					for (BookBean bookBean : list) {
						cacheBookBean(bookBean);
						List<CacheTypeBean> cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("type", cacheType).and().eq("bookId", bookBean.getBookId()).query();
						if (cacheTypeBean != null && cacheTypeBean.size() > 0) {
							for (int i = 0; i < cacheTypeBean.size(); i++) {
								if (cacheTypeBean.get(i).getId() > 0) {
									StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).delete(cacheTypeBean.get(i));
								}
							}
						}
						CacheTypeBean m_cacheTypeBean = new CacheTypeBean();
						m_cacheTypeBean.setBookId(bookBean.getBookId());
						m_cacheTypeBean.setType(cacheType);
						StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).create(m_cacheTypeBean);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	private static void cacheBookBean(BookBean bookBean) {
		try {
			BookBean tempBookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(bookBean.getBookId());
			if (tempBookBean == null) {
				bookBean.setCreateTime(new Date());
				StaticReference.mutilDBSupport.getDao(BookBean.class).createOrUpdate(bookBean);
				List<SelectionVo> mList = bookBean.getSectionList();
				for (SelectionVo vo : mList) {
					SelectionVo _temp = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("sectionId", vo.getSectionId()).queryForFirst();
					if (_temp == null) {
						vo.setBookId(bookBean.getBookId());
						StaticReference.mutilDBSupport.getDao(SelectionVo.class).createOrUpdate(vo);
					}
				}
			} else {
				tempBookBean.setIsRead(bookBean.getIsRead());
				tempBookBean.setCommentNum(bookBean.getCommentNum());
				tempBookBean.setListenNum(bookBean.getListenNum());
				tempBookBean.setNeedBuy(bookBean.getNeedBuy());
				tempBookBean.setCanRead(bookBean.getCanRead());
				tempBookBean.setPriceTag(bookBean.getPriceTag());
				tempBookBean.setIsNew(bookBean.getIsNew());
				tempBookBean.setFree(bookBean.getFree());
				tempBookBean.setCommentNum(bookBean.getCommentNum());
				if (TextUtils.isEmpty(tempBookBean.getNationImg()) && !TextUtils.isEmpty(bookBean.getNationImg())) {
					tempBookBean.setNationImg(bookBean.getNationImg());
				}
				StaticReference.mutilDBSupport.getDao(BookBean.class).update(tempBookBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取最新或周top10
	 * @param cacheType
	 */
	public static ArrayList<BookBean> getNewOrTopBookListCache(int cacheType){
		try {
			GenericRawResults<BookBean> result = StaticReference.mutilDBSupport.getDao(BookBean.class)
					.queryRaw("select a.about,a.author,a.slogan,a.bookName,a.commentNum,a.createTime,a.nationImg, a.faceImg, a.faceimgplay,a.favorNum,a.goldword,a.listenNum,a.isfavor,a.ensize,a.prosize,a.doubisize,a.bookid,a.isRead,a.isNew,a.canRead,a.free,a.priceTag,a.needBuy  from book a inner join  cachetype b on a.bookId = b.bookId and b.type = "+ cacheType +" order by b.tId ",
							new RawRowMapper<BookBean>(){
								@Override
								public BookBean mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
									BookBean bookBean = new BookBean();
									bookBean.setAbout(resultColumns[0]);
									bookBean.setAuthor(resultColumns[1]);
									bookBean.setSlogan(resultColumns[2]);
									bookBean.setBookName(resultColumns[3]);
									bookBean.setCommentNum(resultColumns[4]);
									bookBean.setCreateTime(new Date());
									bookBean.setNationImg(resultColumns[6]);
									bookBean.setFaceImg(resultColumns[7]);
									bookBean.setFaceImgPlay(resultColumns[8]);
									bookBean.setFavorNum(resultColumns[9]);
									bookBean.setGoldWord(resultColumns[10]);
									bookBean.setListenNum(resultColumns[11]);
									bookBean.setIsFavor(Integer.parseInt(resultColumns[12]));
									bookBean.setEnSize(Integer.parseInt(resultColumns[13]));
									bookBean.setProSize(Integer.parseInt(resultColumns[14]));
									bookBean.setDoubiSize(Integer.parseInt(resultColumns[15]));
									bookBean.setBookId(Integer.parseInt(resultColumns[16]));
									bookBean.setIsRead(CommonMethod.parseInt(resultColumns[17], 1));
									bookBean.setIsNew(CommonMethod.parseInt(resultColumns[18], 1));
									bookBean.setCanRead(CommonMethod.parseInt(resultColumns[19], 2));
									bookBean.setFree(CommonMethod.parseInt(resultColumns[20], 1));
									bookBean.setPriceTag(resultColumns[21]);
									bookBean.setNeedBuy(CommonMethod.parseInt(resultColumns[22], 1));
									return bookBean;
								}});
			
			ArrayList<BookBean> bookList = (ArrayList<BookBean>) result.getResults();
			for (int i = 0; i < bookList.size(); i++) {
				List<SelectionVo> selectionList = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("bookId", bookList.get(i).getBookId()).query();
				if (selectionList != null) {
					bookList.get(i).setSectionList(selectionList);
				}
			}
			return bookList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	
	/**
	 * 缓存 BannerBean 到本地
	 * @param list
	 */
	public static void cacheBanner2Local(final ArrayList<BannerItem> list){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
//					StaticReference.mutilDBSupport.getDao(BannerItem.class).executeRaw("DELETE FROM tb_banner ");
					ArrayList<BannerItem> bannerList = (ArrayList<BannerItem>) StaticReference.mutilDBSupport.getDao(BannerItem.class).queryForAll();
					if (bannerList != null && bannerList.size() > 0) {
						StaticReference.mutilDBSupport.getDao(BannerItem.class).delete(bannerList);
					}
					if (list != null) {
						for (BannerItem bannerItem : list) {
							switch (bannerItem.getType()) {
							case 1:
								if (bannerItem.getBookModel() != null) {
									BookBean bookBean = bannerItem.getBookModel();
									BookBean tempBookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(bookBean.getBookId());
									if (tempBookBean == null) {
										bookBean.setCreateTime(new Date());
										StaticReference.mutilDBSupport.getDao(BookBean.class).create(bookBean);
										List<SelectionVo> list = bookBean.getSectionList();
										for (SelectionVo vo : list) {
											SelectionVo _temp = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("sectionId", vo.getSectionId()).queryForFirst();
											if (_temp == null) {
												vo.setBookId(bookBean.getBookId());
												StaticReference.mutilDBSupport.getDao(SelectionVo.class).create(vo);
											}
										}
									}
									bannerItem.setId(bookBean.getBookId());
								}
								break;
							case 2:
								if (bannerItem.getHofModel() != null) {
									StaticReference.mutilDBSupport.getDao(VideoBean.class).createOrUpdate(bannerItem.getHofModel());
									bannerItem.setId(bannerItem.getHofModel().getId());
								}
								break;
							case 3:
								if (bannerItem.getH5Model() != null) {
									StaticReference.mutilDBSupport.getDao(Banner_H5.class).createOrUpdate(bannerItem.getH5Model());
									bannerItem.setId(bannerItem.getH5Model().getId());
								}
								break;
							case 4:
								if (bannerItem.getBookListModel() != null) {
									StaticReference.mutilDBSupport.getDao(BookListModel.class).createOrUpdate(bannerItem.getBookListModel());
									bannerItem.setId(bannerItem.getBookListModel().getListId());
								}
								break;
							}
							List<BannerItem> items = StaticReference.mutilDBSupport.getDao(BannerItem.class).queryBuilder().where().eq("id", bannerItem.getId()).and().eq("type", bannerItem.getType()).and().eq("bannerImg", bannerItem.getBannerImg()).query();
							if (items == null || (items != null && items.size() == 0)) {
								StaticReference.mutilDBSupport.getDao(BannerItem.class).create(bannerItem);
							}
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	
	
	/**
	 * 获取本地的 BannerBean
	 * @param handle
	 */
	public static void getbannerCache(final Handler handle) {
		Pool.getPool().execute(new Runnable() {

			@Override
			public void run() {
				try {
					 ArrayList<BannerItem>  list = (ArrayList<BannerItem>) StaticReference.mutilDBSupport.getDao(BannerItem.class).queryForAll();
					 if (list != null) {
						 for (int i = 0; i < list.size(); i++) {
							switch (list.get(i).getType()) {
							case 1:
								BookBean bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", list.get(i).getId()).queryForFirst();
								if (bookBean != null) {
									List<SelectionVo> vo = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("bookId", bookBean.getBookId()).query();
									bookBean.setSectionList(vo);
									list.get(i).setBookModel(bookBean);
								}
								break;
							case 2:
								VideoBean videoBean = StaticReference.mutilDBSupport.getDao(VideoBean.class).queryBuilder().where().eq("id", list.get(i).getId()).queryForFirst();
								if (videoBean != null) {
									list.get(i).setHofModel(videoBean);
								}
								break;
							case 3:
								Banner_H5 banner_H5 = StaticReference.mutilDBSupport.getDao(Banner_H5.class).queryBuilder().where().eq("id", list.get(i).getId()).queryForFirst();
								if (banner_H5 != null) {
									list.get(i).setH5Model(banner_H5);
								}
								break;
							}
						}
					 }
					 if (list != null && list.size() > 0) {
						 handle.obtainMessage(10,list).sendToTarget();
					 }else {
						handle.sendEmptyMessage(11);
					 }
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 缓存阅读模式数据到本地
	 * @param modeBean
	 */
	public static void cacheReadMode2Local(final ReadModeBean modeBean){
		Pool.getPool().execute(new  Runnable() {
			@Override
			public void run() {
				if (modeBean != null) {
					try {
						BookBean  bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", modeBean.getBookId()).queryForFirst();
						if (bookBean != null) {
							bookBean.setBookId(modeBean.getBookId());
							bookBean.setFaceImg(modeBean.getFaceImg());
							bookBean.setNationImg(modeBean.getNationImg());
							bookBean.setBookName(modeBean.getBookName());
							bookBean.setListenNum(modeBean.getListenNum());
							bookBean.setFavorNum(modeBean.getFavorNum());
							bookBean.setCommentNum(modeBean.getCommentNum());
							bookBean.setProSize(modeBean.getProSize());
							bookBean.setDoubiSize(modeBean.getDoubiSize());
							bookBean.setEnSize(modeBean.getEnSize());
							bookBean.setAbout(modeBean.getAbout());
							bookBean.setGoldWord(modeBean.getGoldWord());
							bookBean.setAuthor(modeBean.getAuthor());
							bookBean.setIsFavor(modeBean.getIsFavor());
							bookBean.setFaceImgPlay(modeBean.getFaceImgPlay());
							bookBean.setIsRead(modeBean.getIsRead());
							bookBean.setSlogan(modeBean.getSlogan());
							bookBean.setIsNew(modeBean.getIsNew());
							bookBean.setCanRead(modeBean.getCanRead());
							bookBean.setFree(modeBean.getFree());
							bookBean.setPriceTag(modeBean.getPriceTag());
							bookBean.setNeedBuy(modeBean.getNeedBuy());
							bookBean.update();
						}else {
							bookBean = new BookBean();
							bookBean.setCreateTime(new Date());
							bookBean.setBookId(modeBean.getBookId());
							bookBean.setFaceImg(modeBean.getFaceImg());
							bookBean.setNationImg(modeBean.getNationImg());
							bookBean.setBookName(modeBean.getBookName());
							bookBean.setListenNum(modeBean.getListenNum());
							bookBean.setFavorNum(modeBean.getFavorNum());
							bookBean.setCommentNum(modeBean.getCommentNum());
							bookBean.setProSize(modeBean.getProSize());
							bookBean.setDoubiSize(modeBean.getDoubiSize());
							bookBean.setEnSize(modeBean.getEnSize());
							bookBean.setAbout(modeBean.getAbout());
							bookBean.setGoldWord(modeBean.getGoldWord());
							bookBean.setAuthor(modeBean.getAuthor());
							bookBean.setIsFavor(modeBean.getIsFavor());
							bookBean.setFaceImgPlay(modeBean.getFaceImgPlay());
							bookBean.setIsRead(modeBean.getIsRead());
							bookBean.setSlogan(modeBean.getSlogan());
							bookBean.setIsNew(modeBean.getIsNew());
							bookBean.setCanRead(modeBean.getCanRead());
							bookBean.setFree(modeBean.getFree());
							bookBean.setPriceTag(modeBean.getPriceTag());
							bookBean.setNeedBuy(modeBean.getNeedBuy());
							StaticReference.mutilDBSupport.getDao(BookBean.class).create(bookBean);
						}
						for (SelectionVo element : modeBean.getSectionList()) {
							StaticReference.mutilDBSupport.getDao(SelectionVo.class).createOrUpdate(element);
							/*SelectionVo vo = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("sectionId", element.getSectionId()).queryForFirst();
							if (vo != null) {
								vo.setDescription(element.getDescription());
								vo.setDescriptionEn(element.getDescriptionEn());
								vo.setTitleEn(element.getTitleEn());
								vo.setTitle(element.getTitle());
								vo.setProSize(element.getProSize());
								vo.setEnSize(element.getEnSize());
								vo.setDoubiSize(element.getDoubiSize());
								vo.setProSec(element.getProSec());
								vo.setDoubiSec(element.getDoubiSec());
								vo.setEnSec(element.getEnSec());
								vo.update();
							}else {
								StaticReference.mutilDBSupport.getDao(SelectionVo.class).create(element);
							}*/
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
	/**
	 * 修改播放时的图片
	 * @param bookId
	 * @param faceImgPlay
	 */
	public static void cacheFaceImgPlay2Local(final int bookId,final String faceImgPlay){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					BookBean  bookBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", bookId).queryForFirst();
					if (bookBean != null) {
						bookBean.setFaceImgPlay(faceImgPlay);
						bookBean.update();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 设置本地是否收藏
	 * @param bookId
	 * @param isFavor
	 */
	public static void cacheLocalIsFavor(int bookId,int isFavor){
		try {
			BookBean bean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", bookId).queryForFirst();
			if (bean != null) {
				bean.setIsFavor(isFavor);
				bean.update();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置本地是否已读
	 * @param bookId
	 * @param isFavor
	 */
	public static void cacheLocalIsHasRead(int bookId,int hasRead){
		try {
			BookBean bean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", bookId).queryForFirst();
			if (bean != null) {
				bean.setIsRead(hasRead);
				bean.update();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置真在播放的书
	 */
	public static void setPlayCacheContinueBook(BookBean bean){
		try {
			if (StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", bean.getBookId()).queryForFirst() == null) {
				bean.setCreateTime(new Date());
				StaticReference.mutilDBSupport.getDao(BookBean.class).createOrUpdate(bean);
				List<SelectionVo> list = bean.getSectionList();
				for (SelectionVo vo : list) {
					SelectionVo _temp = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("sectionId", vo.getSectionId()).queryForFirst();
					if (_temp == null) {
						vo.setBookId(bean.getBookId());
						StaticReference.mutilDBSupport.getDao(SelectionVo.class).createOrUpdate(vo);
					}
				}
			}
			/*CacheTypeBean cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("type", -1).queryForFirst();
			if (cacheTypeBean == null) {
				cacheTypeBean = new CacheTypeBean();
				cacheTypeBean.setBookId(bean.getBookId());
				cacheTypeBean.setType(-1);
				cacheTypeBean.create();
			}else {
				cacheTypeBean.setBookId(bean.getBookId());
				cacheTypeBean.update();
			}*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取正在播放的书
	 */
	public static BookBean getPlayCacheContinueBook(int bookId){
		BookBean localBean = null;
		try {
			localBean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryBuilder().where().eq("bookId", bookId).queryForFirst();
			if (localBean != null) {
				List<SelectionVo> vo = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("bookId", bookId).query();
				localBean.setSectionList(vo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return localBean;
	}
	
	public static void addBookType(final List<BookTypeBean> list){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				if (list != null && list.size() > 0) {
					for (BookTypeBean bookTypeBean : list) {
						try {
							bookTypeBean.setCreateTime(new Date());
							StaticReference.mutilDBSupport.getDao(BookTypeBean.class).createOrUpdate(bookTypeBean);
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}				
			}
		});
	}
	
	public static void getAllBookType(final Handle handle){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					List<BookTypeBean> list = StaticReference.mutilDBSupport.getDao(BookTypeBean.class).queryBuilder().orderBy("createTime", true).query();
					if (list != null && list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							if (list.get(i).getTypeId() == 0) {
								BookTypeBean bean = list.remove(i);
								list.add(0,bean);
								break;
							}
						}
					}
					handle.obtainMessage(1, list).sendToTarget();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 缓存用户信息到本地
	 * @param userInfoBean
	 */
	public static void cacheUserInfo2Local(final UserInfoBean userInfoBean){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					StaticReference.mutilDBSupport.getDao(UserInfoBean.class).createOrUpdate(userInfoBean);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 获取用户信息
	 * @param userId
	 * @return
	 */
	public static UserInfoBean getUserInfo(int userId){
		UserInfoBean userInfoBean = null;
		try {
			userInfoBean = StaticReference.mutilDBSupport.getDao(UserInfoBean.class).queryForId(userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userInfoBean;
	}
	
	/**
	 * 缓存用户信息到本地
	 * @param userInfoBean
	 */
	public static void cacheUserInfoByJson(final int userId, final String userBaseInfo){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					UserInfoTable userInfoTable = new UserInfoTable();
					userInfoTable.setId(userId);
					userInfoTable.setContent(userBaseInfo);
					StaticReference.mutilDBSupport.getDao(UserInfoTable.class).createOrUpdate(userInfoTable);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * 缓存用户信息到本地
	 * @param userInfoBean
	 */
	public static UserInfoTable getUserInfoByJson(int userId){
		UserInfoTable userInfoTable = null;
		try {
			userInfoTable = StaticReference.mutilDBSupport.getDao(UserInfoTable.class).queryForId(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userInfoTable;
	}
	
	/***
	 * 
	 * @param context
	 * @param bookId
	 * @param handler
	 * @param needPause 0需要， 1，不需要
	 */
	public static void getBookBeanById(final Context context,final int bookId,final Handler handler,final int needPause){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					BookBean bean = StaticReference.mutilDBSupport.getDao(BookBean.class).queryForId(bookId);
					if (bean != null) {
						List<SelectionVo> list = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("bookId", bookId).query();
						if (list != null) {
							bean.setSectionList(list);
						}
						handler.obtainMessage(HttpRequestHelper.GET_LOCAL_BOOKBEAN_SUCCESS,needPause,0,bean).sendToTarget();
					} else {
						bean = new BookBean();
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(context, HttpRequestHelper.USERID, 0))));
						params.add(new BasicNameValuePair(HttpRequestHelper.BOOKID, String.valueOf(bookId)));
						ResponeResult responeResult = new ResponeResult();
						try {
							HttpEntity entity = HttpHelper.getInfoByPost(HttpRequestHelper.LISTEN_MODE, params, context);
							String _result = HttpHelper.entity2String(entity);
							JSONObject jsonResult = new JSONObject(_result);
							JSONObject result = jsonResult.getJSONObject(HttpCommonInfo.RESULT);
							int code = result.getInt(HttpCommonInfo.RESULT_CODE);
							String msg = result.getString(HttpCommonInfo.RESULT_MSG);
							responeResult.setCode(code);
							responeResult.setMsg(msg);
							if (code == 0) {
								responeResult.setData(jsonResult.getString(HttpCommonInfo.DATA));
								ListenModeBean listenModeBean = new Gson().fromJson(responeResult.getData(), new TypeToken<ListenModeBean>() {}.getType());
								bean.setBookId(listenModeBean.getBookId());
								bean.setSectionList(listenModeBean.getSectionList());
								bean.setFaceImg(listenModeBean.getFaceImg());
								bean.setFaceImgPlay(listenModeBean.getFaceImgPlay());
								bean.setBookName(listenModeBean.getBookName());
								handler.obtainMessage(HttpRequestHelper.GET_LOCAL_BOOKBEAN_SUCCESS,needPause,0, bean).sendToTarget();
							} else {
								handler.sendEmptyMessage(-100);
							}
						} catch (ClientProtocolException e) {
							e.printStackTrace();
							responeResult = null;
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
							responeResult = null;
						} catch (IOException e) {
							e.printStackTrace();
							responeResult = null;
						} catch (JSONException e) {
							e.printStackTrace();
							responeResult = null;
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static void getBookBeanById(Context context,int bookId,Handler handler){
		getBookBeanById(context, bookId, handler, 0);
	}
	
	
	/**
	 * 缓存视屏到本地
	 * @param list
	 */
	public static void cacheVideo2Local(final List<VideoBean> list){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				StaticReference.mutilDBSupport.getDao(VideoBean.class).clearObjectCache();
				if (list != null && list.size() > 0) {
					for (VideoBean videoBean : list) {
						try {
							videoBean.setCreateTime(new Date());
							StaticReference.mutilDBSupport.getDao(VideoBean.class).createOrUpdate(videoBean);
						} catch (SQLException e) {
							e.printStackTrace();
						}						
					}
				}
			}
		});
	}
	
	/**
	 * 获取本地视屏
	 * @param handler
	 * @param success
	 * @param fail
	 */
	public static void getVideoCache(final Handler handler,final int success){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					List<VideoBean> list = StaticReference.mutilDBSupport.getDao(VideoBean.class).queryBuilder().orderBy("createTime", true).query();
					if (list == null) {
						list = new ArrayList<VideoBean>();
					}
					handler.obtainMessage(success, list).sendToTarget();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
